\section{\TikZ\ (Part 3)}

In this video, we're going to build on what we did in the previous ones to create some slightly more complex images. We'll start off by talking a bit more about nodes.

\begin{center}
  \fbox{\includegraphics[width=0.95\linewidth]{img-12-1}}
\end{center}

So far, we've only put short pieces of text into the nodes. But nodes are far more flexible objects, and we can put lots of things inside of them. Here's an example of a tabular environment inside of a node. And here's an image inside of a node. If we wanted, we could even put a tikzpicture inside of a node.

\begin{center}
  \fbox{\includegraphics[width=0.65\linewidth]{img-12-2}}
\end{center}

Mathematical notation is most easily done using inline notation. You can still force it to use display style presentation, but you will want to enter the notation as if it's inline. The reason you have to use inline mode is because display style mode needs to know the width of page for it to center the text properly, and the default behavior for nodes is to stretch to fit the contents.

\begin{center}
  \fbox{\includegraphics[width=0.95\linewidth]{img-12-3}}
\end{center}

You can actually declare the width of a node as an optional argument. The default behavior is that the text will be left justified. If you do this, you can actually create enough of a page-like environment that display style math mode will work properly. In fact, if you set a \texttt{text width} for the node, you will be able to put even more things inside the node. This will open up \texttt{itemize} and \texttt{enumerate} environments as well as left, right, and center justification.

\begin{center}
  \fbox{\includegraphics[width=0.95\linewidth]{img-12-4}}
\end{center}

Next, we're going to look at creating a basic graph. Before getting started, I'll quickly note that some people prefer using \texttt{pgfplots} for this because it's a more robust approach, but I'm just going to stick with just \TikZ\ for this video.

To begin with, we need to create a coordinate axis. We will use a domain of $[-4,4]$ and codomain of $[-1,5]$ for the graph, but we'll put a little extra on the coordinate axes so that they stick out a little bit beyond the graphing area. We'll create nodes to label the axes, and we'll need to shift the anchor in order for the text to appear in the correct positions.

If we wanted to add in a coordinate grid, we can use the \texttt{grid} command. The grid command works like a rectangle. We will want to put this at the beginning of the code because everything else, including the coordinate axes, is drawn on top of the grid. And we will use \texttt{ultra thin} lines and a \texttt{lightgray} color so that it's not too distracting. You can also change the step size of the grid if you wanted to.

We will now make our graph. To do this, we'll introduce a new command, which is the \verb|plot| command. Inside of the \verb|\draw| command, we need to specify the domain and the variable for our plot. Notice that the variable has a backslash in front of it. We will also need to specify the number of sample points that \LaTeX\ will use to create the graph. I usually start with \texttt{samples=50} and adjust it from there. I also throw in the \texttt{smooth} command, which will try to smooth out the curve a little bit. If you pick enough samples, this usually isn't an issue. In order to make this stand out more, we're going to make it blue and pick a thick line width.

This plotting function is actually a parametric plot, but since we're graphing the function $y = f(x)$, the coordinate $x$ will be our parameter. And in this case, we are going to graph the parabola $y = x^2$.

We can see that the graph goes well beyond the axes. We can try to change the domain to fix this, but that can be very tedious if the function is more complicated. Instead, we can use clipping to keep only the parts of the graph that are inside the region we care about. The shape can be any closed figure, including polygons and circles. Everything after the \verb|\clip| command will be cut. But this will cause problems for us because we really only want the graph to be clipped.

To take care of this, we are going to create a \verb|scope| environment. The code inside of the scope environment is drawn on its own, and then inserted into the larger picture. We can also apply transformations to the scope, which is handy if you have complex objects that you want to move as a group. But we won't be using any of that for this graph.

Let's label the graph. We'll put the label off to the left side. In order to make it stand out, we'll draw the border, we'll make the text in blue, and we'll fill the background white so that the coordinate grid doesn't show through.

We're now going to mark the point $(2,4)$ on the graph. To do this, we will use a filled circle. Let's make it red. I usually use a 1 mm radius, but what you use is up to you. Let's put the coordinates for this point off the right. Notice that the text is red because the \verb|\draw| command is red. We can change this to black by changing the node's color as an optional argument. If we tried to fill the node white, it would end up drawing over both the graph and the dot as well. We could fix this by drawing the coordinates much earlier in the drawing, translating the coordinates further to the right, or we can just live with the line cutting through the text.

Let's draw a dashed line from our point to the coordinate axes and label the input and output values there. This can be done as two separate lines, but it's more compact to do it as one line using orthogonal paths.

Lastly, let's draw the tangent line. Unfortunately, there's no way that I know of to automatically calculate the tangent line here, so we'll have to use calculus to find the equation. We end up with $y = 4x - 4$. We can place this part inside of the scope with the clipping without having to figure out the exact coordinates where the line leaves the grid.

And this gives us a fairly nice graph in \TikZ\ without too much hassle. What's nice about this is that if we just relabel things a little bit and get rid of the grid, we have a generic parabola with tangent line. Once you get used to the syntax, this becomes a fairly easy process.

\begin{center}
  \fbox{\includegraphics[width=0.95\linewidth]{img-12-5}}
\end{center}

It's worth noting that when we're inside of the plot command, we also have access to the sine, cosine, and tangent functions, as well as the exponential function, the natural logarithm function, and the absolute value function. The trigonometric functions are functions of degrees, so you will probably want to include a factor to convert your angle from radians to degrees. Also, because the tangent function has vertical asymptotes, you may want to graph each branch separately.

The \TikZ\ manual is over 1200 pages in length. It would take a pretty dedicated effort to work through it all. Fortunately, there's a much easier way to learn new things in \TikZ. There is \href{http://www.texample.net/tikz/examples/}{an entire webpage} dedicated to \TikZ\ examples. It even has links so that you can open the code directly in Overleaf and start playing around with it.

And that's really where the learning happens in \TikZ. You kind of just have to play with the code. The last couple videos have shown you a basic framework to understand how \TikZ\ works, but there's too much going on to present it all to you. Here are just a couple of highlights of ideas you can search for on your own.

\begin{center}
  \fbox{\includegraphics[width=0.95\linewidth]{img-12-6}}
\end{center}

\begin{itemize}
  \item Coordinates: There is a more powerful and more flexible way of drawing using named coordinates. If you're doing a diagram with lots of nodes, you can define the connections by the name of the point, and then if you have to move your point around all of the connections will update automatically.
  \begin{center}
    \fbox{\includegraphics[width=0.95\linewidth]{img-12-7}}
  \end{center}
  \item For loops: If you have a repeating pattern, you can create a for loop so that you don't need to retype the code over and over again. The variables can be used both in the coordinates and as text objects.
  \begin{center}
    \fbox{\includegraphics[width=0.95\linewidth]{img-12-8}}
  \end{center}
  \item Fancy shading: \TikZ\ is able to color regions using a color gradient instead of a solid color.
  \begin{center}
    \fbox{\includegraphics[width=0.95\linewidth]{img-12-9}}
  \end{center}
  \item Opacity: Opacity is a measure of how well you can see through an image. This is great for overlaying objects on top of each other without completely hiding everything.
\end{itemize}

There is so much more, including three dimensional coordinate systems, fancy node diagrams, mind maps, and trees. Furthermore, if you happen to use Geogebra, there's an export to \TikZ\ command that will let you export your diagrams and graphs. But you'll have to explore those on your own.